const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    shopData: {},
    userInfo: {},
    orderInfo: {},
    order_id: 143,
    showMask: false,
    reasonList: [],
    reason_id: '',
    remark: '',
    reason_id_na: '',
    reasonText: '',
    inputText: '选填',
  },
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    let shopData = wx.getStorageSync('shop_data')
    this.setData({
      shopData: shopData,
      userInfo: userInfo
    })
    if (options.order_id) {
      this.setData({
        order_id: options.order_id
      })
    }

    this.setData({
      inputText: '选填'
    })
    this.init()
  },

  init: function () {
    this.getOrderDetail();
    this.getReason();
  },

  // 获取订单详情
  getOrderDetail: function () {
    let that = this
    let order_id = this.data.order_id
    api.ajax(service.url('sellerOrderDetail'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      order_id: this.data.order_id
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          orderInfo: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取拒绝理由
  getReason: function () {
    let that = this
    api.ajax(service.url('getSellerReasonList'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      order_id: this.data.order_id
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          reasonList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  hidePon: function () {
    this.setData({
      showMask: false
    })
  },
  showPon: function () {
    this.setData({
      showMask: true
    })
  },

  // 得到拒绝理由
  radioChange: function (e) {
    let reason_id = e.detail.value
    this.setData({
      reason_id_na: reason_id
    })
  },

  // 设置理由
  choicReason: function () {
    let arr = this.data.reasonList
    let str = ''
    for (var i = 0, len = arr.length; i < len; i++) {
      if (arr[i].order_seller_reason_id == this.data.reason_id_na) {
        str = arr[i].content
      }
    }
    this.setData({
      reason_id: this.data.reason_id_na,
      showMask: false,
      reasonText: str
    })
    let reason_id = this.data.reason_id
    if (reason_id == 4) {
      this.setData({
        inputText: '请填写拒绝说明'
      })
    } else {
      this.setData({
        inputText: '选填'
      })
    }
  },

  // 获取拒绝说明
  getRemark: function (e) {
    let val = e.detail.value
    this.setData({
      reason_id: val
    })
  },

  // 确定拒绝
  suerRefuse: function () {
    let that = this
    if (that.data.reason_id == '' || that.data.reason_id == null) {
      api.toast('请选择拒绝理由')
      return
    }
    if (that.data.reason_id == 4) {
      if (that.data.remark == '' || that.data.remark == null) {
        api.toast('拒绝原因为其他时，必须填写退款说明')
        return
      }
    }
    api.ajax(service.url('refuseReturnOrder'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      reply: this.data.reason_id,
      order_return_id: this.data.orderInfo.refund.order_return_id,
      reply_remark: this.data.remark
    }, function (res) {
      if (res.code == 0) {
        api.toast('操作成功')
        setTimeout(function () {
          getCurrentPages()[getCurrentPages().length - 2].onLoad()
          getCurrentPages()[getCurrentPages().length - 1].onLoad()
          wx.redirectTo({
            url: "../shop_order/shop_order"
          });
        },800)
      } else {
        api.toast(res.message)
      }
    })
  },

  // 前往订单详情
  toGoodsDetail: function (e) {
    let goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../goods_detail/goods_detail?goods_id=${goods_id}`,
    })
  }
})